package org.javaboy.shutdown_demo;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.PrintWriter;

@RestController
public class HelloController {

    @Autowired
    ApplicationContext ctx;

    @PostMapping("/bye")
    public void shutdown(HttpServletResponse resp) throws IOException {
        PrintWriter out = resp.getWriter();
        out.write("bye");
        out.flush();
        out.close();
//        ((GenericApplicationContext) ctx).close();
        int exit = SpringApplication.exit(ctx);
        System.exit(exit);
    }
}
